package com.deparse.mydialog.listview;

import android.content.Context;
import android.support.v4.widget.*;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.deparse.mydialog.R;

/**
 * Created by Administrator on 2016/7/20.
 */
public class PullRefreshListView extends LinearLayout {
    private View contentView;
    private ListView listView;
    private LayoutInflater layoutInflater;
    private View footer;
    private SwipeRefreshLayout refresh;
    private boolean loadfinish = true; // 指示数据是否加载完成
    private OnLoadListener loadListener;

    public PullRefreshListView(Context context) {
        super(context);
        init(context);
    }

    public PullRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PullRefreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        layoutInflater = LayoutInflater.from(context);

        footer = layoutInflater.inflate(R.layout.foot_view, null);
        contentView = inflate(context, R.layout.activity_swipe_refresh, null);
        addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        listView = (ListView) contentView.findViewById(R.id.listView);
        refresh = (android.support.v4.widget.SwipeRefreshLayout) contentView.findViewById(R.id.refresh);

        listView.addFooterView(footer);
        listView.setOnScrollListener(new ScrollListener());

        refresh.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);

    }

    private final class ScrollListener implements AbsListView.OnScrollListener {

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                             int visibleItemCount, int totalItemCount) {

            final int loadtotal = totalItemCount;
            int lastItemid = listView.getLastVisiblePosition(); // 获取当前屏幕最后Item的ID
            if (totalItemCount == 40) {
                return;
            }
            if ((lastItemid + 1) == totalItemCount) { // 达到数据的最后一条记录
                if (totalItemCount > 0) {
                    // 当前页
//                    int currentpage = totalItemCount % number == 0 ? totalItemCount
//                            / number
//                            : totalItemCount / number + 1;
//                    int nextpage = currentpage + 1; // 下一页
//                    if (nextpage <= maxpage && loadfinish) {
                    if (loadfinish && !refresh.isRefreshing()) {
                        loadfinish = false;
                        listView.addFooterView(footer);
                        // 开一个线程加载数据
                        if (null != loadListener) {
                            loadListener.onLoadListener();
                        }
//                        mHandler.sendEmptyMessageDelayed(1, 1000);
                    }


                }
            }

        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

    }

    public interface OnLoadListener {
        void onLoadListener();
    }

    public void loadFinish() {
        this.loadfinish = true;
    }

    public ListView getListView() {
        return this.listView;
    }

    public View getFooterVIew() {
        return this.footer;
    }

    public SwipeRefreshLayout getRefresh() {
        return this.refresh;
    }

    private void setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener listener) {
        this.refresh.setOnRefreshListener(listener);
    }

    public void setOnLoadListener(OnLoadListener listener) {
        this.loadListener = listener;
    }
}
